/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdlib.h>
#include <limits.h>
#include <ctype.h>

extern "C" size_t strtoi(const char *s, char **endptr, int base, int width, char flag) {
    const char *tmp = s;
    const char *head;
    unsigned long int result = 0;
    int mflag = 1;
    unsigned long int max;
    if(s == NULL) {
        return result;
    }
    if(base > 36) {
        base = 36;
    }
    if(base < 0) {
        base = 0;
    }
    while(isspace(*s)) {
        s++;
    }
    if(*s == '+') {
        s++;
    } else if(*s == '-') {
        mflag = -1;
        s++;
    }
    head = s;
    if(*head == '0') {
        head++;
        if( ((*head == 'X') || (*head == 'x')) && ((base == 0) || (base == 16)) ) {
            head++;
            if(isxdigit(*head) != 0) {
                base = 16;
                s = head;
            }
        } else if(*head >= '1' && *head <= '7') {
            if(base == 0) {
                base = 8;
            }
        } else {
            if(base == 0) {
                base = 10;
            }
        }
    } else if(base == 0) {
        base = 10;
    }
    head = s;
    if(width == 0) {
        width = INT_MAX;
    }
    width -= (int)(head - tmp);
    if(flag & S_FORMAT_UNSIGNED) {
        mflag = 1;
        if(flag & S_FORMAT_LONG) {
            max = ULONG_MAX;
        } else {
            max = UINT_MAX;
        }
    } else {
        if(flag & S_FORMAT_LONG) {
            max = LONG_MAX;
        } else {
            max = INT_MAX;
        }
    }
    while(width-- > 0) {
        long int value;
        if(!isalnum(*s)) {
            break;
        }
        if(isdigit(*s)) {
            value = *s - '0';
        } else {
            value = toupper(*s) - ('A' - 10);
        }
        if(value >= base) {
            break;
        }
        if(result == max) {
            s++;
            continue;
        } else if(result > (max - value) / base) {
            result = max;
            s++;
            continue;
        }
        result = result * base + value;
        s++;
    }
    if(endptr != NULL) {
        if(s == head) {
            *endptr = (char *)tmp;
        } else {
            *endptr = (char *)s;
        }
    }
    return result * (long)mflag;
}
